<?php


/**
 * OWL Widgets Class
 *
 * @package OWL
 * @subpackage Widget
 * @author trung
 */
class Owl_Widgets {

    var $id_base;
    var $name;
    var $widget_description;
    var $widget_classname;
    var $widget_width;
    var $widget_height;

    var $number = false;
    var $id_instance = false;
	

	// Member functions that you must over-ride.

	/** Echo the widget content.
	 *
	 * Subclasses should over-ride this function to generate their widget code.
	 *
	 * @param array $display_args Display arguments including before_title, after_title, before_widget, and after_widget.
	 * @param array $instance The settings for the particular instance of the widget
	 */
	function widget($instance) {
		die('function Owl_Widget::widget() must be over-ridden in a sub-class.');
	}

	/** Update a particular instance.
	 *
	 * This function should check that $new_instance is set correctly.
	 * The newly calculated value of $instance should be returned.
	 * If "false" is returned, the instance won't be saved/updated.
	 *
	 * @param array $new_instance New settings for this instance as input by the user via form()
	 * @param array $old_instance Old settings for this instance
	 * @return array Settings to save or bool false to cancel saving
	 */
	function update($new_instance, $old_instance) {
		return $new_instance;
	}

	/** Echo the settings update form
	 *
	 * @param array $instance Current settings
	 */
	function form($instance) {
		echo '<p>There are no options for this widget.</p>';
		return 'noform';
	}

	// Functions you'll need to call.

	/**
	 * PHP4 constructor
	 */
	function Owl_Widgets( $id_base = false, $name, $widget_description = 'Không có mô tả',$widget_classname = false,$widget_width = 250,$widget_height = 360 ) {
		$this->__construct( $id_base, $name, $widget_description, $widget_classname,$widget_width,$widget_height );
	}

	/**
	 * PHP5 constructor
	 *
	 * @param string $id_base Optional Base ID for the widget, lower case,
	 * if left empty a portion of the widget's class name will be used. Has to be unique.
	 * @param string $name Name for the widget displayed on the configuration page.
	 * @param string $widget_description Description for this widget type.
	 * @param string $widget_classname Classname for this widget type.
         * @param int $widget_width  Width for this widget type, default 250px
         * @param int $widget_height Height for this widget type, default 360px
         *
	 */
	function __construct( $id_base = false, $name, $widget_description = 'Không có mô tả',$widget_classname = false,$widget_width = 250,$widget_height = 360 ) {
		$this->id_base = empty($id_base) ? preg_replace( '/(owl_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
		$this->name = $name;
		$this->option_name = 'widget_' . $this->id_base;
		$this->widget_description = $widget_description;
		$this->widget_width = $widget_width;
                $this->widget_height = $widget_height;
                $this->widget_classname = $widget_classname?$widget_classname:'widget_'.$this->id_base;
	}

	/**
	 * Constructs name attributes for use in form() fields
	 *
	 * This function should be used in form() methods to create name attributes for fields to be saved by update()
	 *
	 * @param string $field_name Field name
	 * @return string Name attribute for $field_name
	 */
	function get_field_name($field_name) {
		return 'widget-' . $this->id_base . '[' . $this->number. '][' . $field_name . ']';
	}

	/**
	 * Constructs id attributes for use in form() fields
	 *
	 * This function should be used in form() methods to create id attributes for fields to be saved by update()
	 *
	 * @param string $field_name Field name
	 * @return string ID attribute for $field_name
	 */
	function get_field_id($field_name) {
		return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
	}

        function get_settings() {
		$settings = get_option($this->option_name);
		if ( !is_array($settings) )
			$settings = array();
		return $settings;
	}

        function save_settings($settings) {                
		update_option( $this->option_name, $settings );
	}

	function set_instance($number) {
		$this->number = $number;
		$this->id_instance = $this->id_base . '-' . $number;
	}

	/** Generate the actual widget content.
	 *	Just finds the instance and calls widget().
	 *	Do NOT over-ride this function. */
	function build_display($number,$display_args) {
                $this->set_instance($number);
		$setting = $this->get_settings();
		if ( array_key_exists( $this->number, $setting ) ) {
			$instance = $setting[$this->number];
			if ( false !== $instance){
                            extract($display_args,EXTR_SKIP);
                            $title = !empty($instance['title']) ? Owl_Format::esc_html($instance['title']) : $this->name;
                            echo $before_widget;
                            echo '<div class="widget-top">';
                            if ( $title) {
                                echo $before_title . $title . $after_title;
                            }
                            echo '</div>';
                            echo $before_content;
                            $this->widget($instance);
                            echo $after_content;
                            echo $after_widget;
                        }
				
		}
	}

        function build_editable_display($number,$display_args) {
                $this->set_instance($number);
		$setting = $this->get_settings();
		if ( array_key_exists( $this->number, $setting ) ) {
			$instance = $setting[$this->number];
			if ( false !== $instance){
                            
                            extract($display_args,EXTR_SKIP);
                            $title = !empty($instance['title']) ? Owl_Format::esc_html($instance['title']) : $this->name;
                            echo $before_widget;
                            ?>
                                <div class="widget-top">
                                    <div class="widget-title-action">
                                        <a class="widget-info" href="#">info</a>                                        
                                    </div> <?php
                                        if ( $title) {
                                            echo $before_title . $title . $after_title;
                                        }
                                    ?>
                                </div>
                            <?php                          
                               echo '<div class="widget-inside">';
                                   echo $before_content;
                                   $this->widget($instance);
                                   echo $after_content;
                               echo '</div>';
                               echo $after_widget;
                            
                        }

		}
	}

        function build_display_template(){
        ?>
                <div id='widget-<?php echo $this->id_base; ?>-__T__' class='widget-container'>
                                <div class="widget-top">
                                    <div class="widget-title"><?php echo $this->name; ?></div>
                                </div>                                             
                               <div class="widget-inside">
                                   <div class="widget-description">
                                        <?php echo $this->widget_description; ?>
                                   </div>
                               </div>                               
                </div>
        <?php
        }
	/** Deal with changed settings.
	 *	Do NOT over-ride this function. */
	function build_update($number) {
                $all_instances = $this->get_settings();
		if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {			
                            unset($all_instances[$number]);                        
		} else {                
			if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) {
				$settings = $_POST['widget-' . $this->id_base];
                                
			} else
                        {
				return;
			}
                            $new_instance = $settings[$number];
                            //$this->_set($number);
                            $old_instance = isset($all_instances[$number]) && (is_array($all_instances[$number])) ? $all_instances[$number] : array();
                            $instance = $this->update($new_instance, $old_instance);
                            if ( false !== $instance )
                            $all_instances[$number] = $instance;
		}                
		$this->save_settings($all_instances);		
	}

	/** Generate the control form.
	 *	Do NOT over-ride this function. */
	function build_form($number) {
		$settings = $this->get_settings();                
		if ( -1 == $number) {			
			$this->set_instance('__T__');
			$instance = array();
		} else {
			$this->set_instance($number);
			$instance = $settings[$number];
		}

		if ( false !== $instance ) {
                ?>
                    <form action="" method="post">
                    <div class="widget-content">
                    <?php
                    $this->form($instance);
                    ?>
                        </div>                        
                        <input type = "hidden" name="number" class="number" value = "<?php echo $this->number; ?>" />
                        <input type = "hidden" name="id_base" class="id_base"value ="<?php echo $this->id_base; ?>" />
                        <input type = "hidden" name="widget_width" class="widget_width" value ="<?php echo $this->widget_width; ?>" />
                        <input type = "hidden" name="widget_height" class="widget_height" value ="<?php echo $this->widget_height; ?>" />
                        <div class="widget-control-actions">
                            <div class="widget-ajax-feedback">
                                <img src="<?php Owl_Link::image_url(); ?>default/ajax-load-dark.gif" class="widget-ajax-load" alt="Ajax load" style="vertical-align:middle;" />
                            </div>
                            <div class="widget-button-pane">
                                <a class="button widget-control-save" href="#" title="<?php echo __('Save'); ?>"><?php echo __('Save'); ?></a>
                                <a class="button widget-control-delete" href="#" title="<?php echo __('Delete'); ?>"><?php echo __('Delete'); ?></a>
                                <a class="button widget-control-back" href="#" title="<?php echo __('Back'); ?>"><?php echo __('Back'); ?></a>
                            </div>
                            <div style="clear:both;"></div>
                        </div>
                    </form>
                <?php
                }
		
	}
}


?>
